package logic

import (
	"strconv"

	"go-papers/models"
)

func UpdateMomLevel(momId int) {

	// 更新数量
	nowCount := UpdateMomNewUser(momId)
	if nowCount <= 0 {
		return
	}
	nowLevel := GetLevel(nowCount)
	if nowLevel <= 0 {
		return
	}
	//获取当前达人信息
	var data models.Member
	momInfo, err := data.GetByID(momId)
	if err != nil {
		return
	}
	//社区达人 无上一级
	if momInfo.MomUserId == 0 {
		return
	}
	parentMom, err := data.GetByID(momInfo.MomUserId)
	if err != nil {
		return
	}

	// 当前达人 是否在 上一级 level中
	pid := parentMom.MemberId

	var ll models.MemberMomLevel
	info, _ := ll.GetById(pid, momId, nowLevel)
	if info.Id > 0 {
		// 当前徒弟已经 加过等级
		return
	}
	momName := parentMom.RealName

	//加入上级的level 中
	level, _ := strconv.Atoi(parentMom.MomLevel)
	noeLevel := level + 1
	//更新mom level
	var mm models.MemberMom
	_, _ = mm.UpdateMomLevel(pid, noeLevel)

	// 插入level info
	ll.MomLevel = noeLevel
	ll.MomUserId = pid
	ll.PupilId = momId
	ll.PupilLevel = nowLevel
	_, _ = ll.Create()

	//触发订单分佣金
	OrderShareMomHandle(momId, momName, noeLevel)
}
